package com.example.myflowtest

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

class MainViewModel : ViewModel() {

    val _data = MutableStateFlow(0)
    val data = _data.asStateFlow()

    init {
        getData()
    }

    fun getData() {
        var time = 0
        viewModelScope.launch {
            flow {
                while (true) {
                    emit(time)
                    delay(1000L)
                    time++
                }
            }.collect {
                println("----$it")
                _data.value = it
            }

            while ( true){
                println("--------$time")
                _data.emit( time)
//                emit(time)
                delay(1000L)
                time++
            }


        }
    }

}